<template>
  <div>
    <el-row class="login-box">
      <el-col :span="7" class="box">
        <h3>小U商城后台管理系统</h3>
        <!-- model：就是表单的数据 -->
        <!-- rules：就是校验的规则 -->
        <!-- ref：获取这个dom节点，方便操作 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <!-- 和校验有关系，对应他的校验规则 -->
          <el-form-item prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入账号">
              <template slot="prepend"><i class="el-icon-user"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="ruleForm.password" placeholder="请输入密码">
              <template slot="prepend"><i class="el-icon-lock"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="btn-login" type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表单的数据
      ruleForm: {
        username: "",
        password: "",
      },
      // 表单的填写规则
      rules: {
        username: [
          // 第一条规则：required:必填，message:报错信息，trigger（触发器），在blur时触发校验
          { required: true, message: "请输入用户名", trigger: "blur" },
          // 第二条规则：长度，min是6max是10
          {
            min: 6,
            max: 10,
            message: "长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 点击按钮先校验，成功后才会发请求
    submitForm(formName) {
      // this.$refs[formName]=>this.$refs.ruleForm 找到这个dom节点，然后validate（验证）
      this.$refs[formName].validate((valid) => {
        // valid：校验结果
        if (valid) {
          // 检验成功
          this.gotoLogin();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 真的要登录了
    gotoLogin() {
      this.$http.post("/login", data).then((res) => {
        // zzz
      });
    },
  },
};
</script>
<style lang="">
.login-box{
    width: 100vw;
    height:100vh;
    background-image: url("../assets/loginbg.jpeg");
    background-position: center center;
    display: flex;
    justify-content: center;
    align-items: center;
}
.login-box .box{
    background-color: #fff;
    padding: 35px;
    border-radius: 5px;
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, .6);
}
.login-box .box h3{
    text-align: center;
    line-height: 40px;
    font-size: 20px;
    color: #FB6D49;
    margin-bottom: 20px;
}
.btn-login{
    display: block;
    width: 100%;
    margin: 0 auto;
}
</style>